#!/bin/bash

# if error, exit immediately
set -e

# config params
# builder work dir
WORK_DIR=$(cd "$(dirname "$0")" && pwd -P)
echo "builder work dir: ${WORK_DIR}"
# target directory
TARGET_DIR=$(dirname "$(pwd)")
echo "builder target dir: ${TARGET_DIR}"
# pool directory:system packages
POOL_DIR="${TARGET_DIR}/pool"
# product directory:system packages
PRODUCT_DIR="${TARGET_DIR}/product"
# CPU architecture:ARCH="amd64"、ARCH="arm64"
ARCH="amd64"
# SysBoot version
SYS_BOOT_VERSION="1.0.0-${ARCH}"
# SysRootFS version
SYS_ROOTFS_VERSION="1.0.0-${ARCH}"

# clean pool dir
if [ ! -d ${POOL_DIR} ]; then
    mkdir -p ${POOL_DIR}
fi

# clean product dir
if [ ! -d ${PRODUCT_DIR} ]; then
    mkdir -p ${PRODUCT_DIR}
fi

echo "Start Build OS Image..."

# boot(legacy)
source ${WORK_DIR}/boot_legacy_amd64.sh
cp -f ${POOL_DIR}/SysBoot-${SYS_BOOT_VERSION}.tar.gz ${PRODUCT_DIR}/SysBoot.tar.gz
# rootfs
cp -f ${POOL_DIR}/SysRootFS-${SYS_ROOTFS_VERSION}.tar.gz ${PRODUCT_DIR}/SysRootFS.tar.gz
# product install script
cp -f ${WORK_DIR}/product/install_mbr.sh ${PRODUCT_DIR}/install.sh

cd ${PRODUCT_DIR}
tar -czf ${POOL_DIR}/rootfs.tar.gz .

echo "Finish Build OS Image..."
